package com.hwh.bootdemoweb.Netty第六章;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;

/**
 * @author wenhao hu
 * @title: TestPipeline
 * @projectName bootdemoweb
 * @description: TODO
 * @date 2021/12/2310:58
 */
@Slf4j
public class TestPipeline {
    public static void main(String[] args) {
        new ServerBootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler((new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        //1.通过channel 拿到pipeline
                        ChannelPipeline pipeline = ch.pipeline();
                        //2.添加处理器
                        //从字面意义上来看，他是将处理器添加到流水线的最后一个。但实际上这是一个双向链表
                        //head <--> h1 <--> h2 <--> h3 <--> h4 <--> h5 <--> h6 <--> tail 处理器实际上是添加到tail之前
                        pipeline.addLast("h1",new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.debug("1");
                                //将接收到的最原始的消息，转为字符串
                                ByteBuf buf = (ByteBuf) msg;
                                String result = buf.toString(Charset.defaultCharset());
                                //内部调用了一个ctx.fireChannelRead(msg) 会将执行权交给下一个handler
                                super.channelRead(ctx, result);
                            }
                        });
                        pipeline.addLast("h2",new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.debug("2");
                                //将name加工成student的属性
                                Student student = new Student(msg.toString());
                                super.channelRead(ctx, student);
                            }
                        });
                        pipeline.addLast("h3",new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.debug("3，结果是{},class:{}",msg,msg.getClass());
//                                ch.writeAndFlush(ctx.alloc().buffer().writeBytes("hello".getBytes()));
                                //用ctx的writeAndFlush
                                ctx.writeAndFlush(ctx.alloc().buffer().writeBytes("hello".getBytes()));
                            }
                        });
                        pipeline.addLast("h4",new ChannelOutboundHandlerAdapter(){
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                log.debug("4");
                                super.write(ctx, msg, promise);
                            }
                        });
                        pipeline.addLast("h5",new ChannelOutboundHandlerAdapter(){
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                log.debug("5");
                                super.write(ctx, msg, promise);
                            }
                        });
                        pipeline.addLast("h6",new ChannelOutboundHandlerAdapter(){
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                log.debug("6");
                                super.write(ctx, msg, promise);
                            }
                        });
                    }
                })).bind(8080);
    }
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static class Student{
        String name;
    }
}
